﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;
using System.Globalization;

public partial class WebAdmin_AddSite : WebadminBasePage
{
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out Id);
        if (!IsPostBack)
        {
            BindLanguages();
            if (Id == 0)
            {
                
                ltrtitle.Text = "Create New Site";
            }
            else
            {
                SitesHelper cc = new SitesHelper();
                SiteModel cModel = cc.GetById(Id);
                
                ltrtitle.Text = "Site: " + cModel.Name;
                txtName.Text = cModel.Name;
                txtDescription.Text = cModel.Description;
                btnAddSite.Text = "Save Changes";
            }
            
        }
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-ui-1.8.4.custom.min.js", ResolveClientUrl("~/FileManager/js/jquery-ui-1.8.4.custom.min.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery.ui.message.js", ResolveClientUrl("~/Resources/Javascript/jquery.ui.message.js"));
        AddCssLink("FileManager/css/ui-lightness/jquery-ui-1.8.4.custom.css");
    }
    void BindLanguages()
    {
        var q = from cul in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
                orderby cul.DisplayName
                select new
                {
                    cul.Name,
                    cul.NativeName,
                    cul.EnglishName,
                    cul.DisplayName
                };
        CBLLanguages.DataSource = q;
        CBLLanguages.DataTextField = "DisplayName";
        CBLLanguages.DataValueField = "Name";
        CBLLanguages.DataBind();

        using (SiteLanguagesHelper helper = new SiteLanguagesHelper())
        {
            foreach (SiteLanguageModel sm in helper.GetAllBySiteId(Id))
            {
                CBLLanguages.Items.FindByValue(sm.LangCode).Selected = true;
            }
        }

    }
    protected void btnAddSite_Click(object sender, EventArgs e)
    {
        int siteid=Id;
        var SelectedSiteLanguages = from s in CBLLanguages.Items.Cast<ListItem>()
                                    where s.Selected == true
                                    select s;

        using (SitesHelper helper = new SitesHelper())
        {
            //validate unique Site Name
            SiteModel siteAlreadyExist= helper.GetByName(txtName.Text.Trim());
            if (siteAlreadyExist != null && siteAlreadyExist.Id != Id){
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "f", @"$('#message1').message({ dismiss: false, type: 'error', message: 'Site Name already exists in system.' });", true);
                return;
            }
            //Validate Selected Languages
            if (SelectedSiteLanguages.Count() == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "f", @"$('#message1').message({ dismiss: false, type: 'error', message: 'Please select atlease one language' });", true);
                return;
            }

                

            SiteModel sm = new SiteModel();
            sm.Name = txtName.Text;
            sm.Description = txtDescription.Text;
            sm.Id = Id;
            sm.SystemDefined = false;
            if (Id > 0)
                helper.Edit(sm);
            else
                siteid=helper.Save(sm);
        }
        
        
        using (SiteLanguagesHelper helper = new SiteLanguagesHelper())
        {
            IEnumerable<SiteLanguageModel> lstSiteLang = helper.GetAllBySiteId(siteid).ToList();
            foreach (SiteLanguageModel sm in lstSiteLang)
            {
                if (SelectedSiteLanguages.Where(s => s.Value == sm.LangCode).Count() == 0)
                {
                    helper.Delete(sm.Id);
                }
            }

            foreach (ListItem li in SelectedSiteLanguages)
            {
                if (lstSiteLang.Where(c => c.LangCode == li.Value).Count() == 0)
                {
                    SiteLanguageModel sm = new SiteLanguageModel();
                    sm.LangCode = li.Value;
                    sm.LangName = li.Text;
                    sm.SiteId = siteid;
                    CultureInfo ci = new CultureInfo(li.Value);
                    if (ci.TextInfo.IsRightToLeft)
                        sm.Direction = "RTL";
                    helper.Save(sm);
                }
            }
        }
        Response.Redirect("ManageSites.aspx");
    }
}